//
//  HPSearchViewController.m
//  HappyPenguin
//
//  Created by zhuangyihang on 1/31/16.
//  Copyright © 2016 zhuangyihang. All rights reserved.
//

#import "HPSearchViewController.h"
#import "HPSearchViewCell.h"
#import "HPHttpSearchRequest.h"

#import "HPCustomerViewController.h"

@interface HPSearchViewController()<UISearchBarDelegate,UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, strong) NSArray *customers;

@property (nonatomic, strong) UISearchBar *searchBar;
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation HPSearchViewController


- (void)viewDidLoad{
    [super viewDidLoad];
    [self setBaseNavigationBarTitle:@"搜索哥姐"];
    [self addBackButton:nil];
    
    [self setupUI];
}


#pragma mark - Private
- (void)setupUI{
    UISearchBar *bar = [UISearchBar new];
    [self.baseContentView addSubview:bar];
    [bar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.top.width.equalTo(self.baseContentView);
    }];
    bar.delegate = self;
    bar.placeholder = @"请输入哥姐姓名，手机号码，哥姐卡号";
    self.searchBar = bar;
    
    UITableView *tableView = [UITableView new];
    [self.baseContentView addSubview:tableView];
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bar.mas_bottom);
        make.centerX.bottom.width.equalTo(self.baseContentView);
    }];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.tableFooterView = [UIView new];
    
    self.tableView = tableView;
}

- (void)timerSearch{
    [self search:self.searchBar.text];
}

- (void)search:(NSString *)content{
    WS(ws);
    HPHttpSearchRequest *request = [[HPHttpSearchRequest alloc] init];
    request.keyword = content;
    request.responseBlock = ^(HPHttpSearchResponse *response, HttpError *error){
        if (error) {
            
        }else{
            ws.customers = response.results;
            [ws.tableView reloadData];
        }
    };
    
    [request send];
}

#pragma mark - UISearchBarDelegate
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [searchBar resignFirstResponder];
    
    [self search:searchBar.text];
}

- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if (self.timer) {
        [self.timer invalidate];
        self.timer = nil;
    }
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(timerSearch) userInfo:nil repeats:NO];
    
    return YES;
}

#pragma mark - UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.customers.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier = @"HPSearchViewCell";
    HPSearchViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[HPSearchViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
    }
    
    VipQueryObject *obj = [self.customers objectAtIndex:indexPath.row];
    [cell.portraitView sd_setImageWithURL:obj.vipAvatar placeholderImage:[UIConstants defaultPortrait]];
    
    cell.nameLabel.text = obj.vipName;
    cell.descLabel.text = obj.infoString;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    VipQueryObject *obj = [self.customers objectAtIndex:indexPath.row];
    return [HPSearchViewCell getCellHeight:obj.infoString];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    VipQueryObject *obj = [self.customers objectAtIndex:indexPath.row];
    HPCustomerViewController *v = [[HPCustomerViewController alloc] init];
    v.vipId = obj.vipId;
    [self.navigationController pushViewController:v animated:YES];
}
@end
